<script setup>
import { ref, onMounted } from "vue";
import { useRouter } from "vue-router";
import axios from "axios";
const router = useRouter();
// 跳转页面
const jumpTo = (path) => {
  router.push(path);
};

const getCollectionProductList = async () => {
  try {
    const res = await axios.get(`${apiUrl}/collect/user?page=1&limit=100`, {
      headers: {
        "Authori-zation": `Bearer ${getToken()}`,
      },
    });

    if (res.data.status === 200) {
      collectionProductList.value = res.data.data.list;
    }
  } catch (e) {}
};

// 全选
let selectAll = ref(false);

// 产品列表
const collectionProductList = ref([]);

// 全选
const selAll = (val) => {
  productList.value.forEach((item) => {
    item.selected = val;
  });
};

// 修改选择
const changeSelect = () => {
  console.log("修改");
  let allIsSelect = true;
  productList.value.forEach((item) => {
    if (item.selected === false) {
      allIsSelect = false;
    }
  });

  selectAll.value = allIsSelect;
};

// 总价
let total = ref(500);

// 删除
const deleteProduct = async (ids) => {
  try {
    const res = await axios.post(
      `${apiUrl}/collect/del`,
      { category: "product", id: ids },
      {
        headers: {
          "Authori-zation": `Bearer ${getToken()}`,
        },
      }
    );

    if (res.data.status === 200) {
      getCollectionProductList();
    }
  } catch (e) {}
};
// 批量删除商品
const delAll = (isAll) => {
  let ids = [];

  productList.value.forEach((item) => {
    if (isAll) {
      // 如果是删除全部
      ids.push(item.id);
    } else {
      if (item.selected === true) {
        // 如果是删除选中的
        ids.push(item.id);
      }
    }
  });
  deleteProduct(ids);
};

onMounted(() => {
  getCollectionProductList();
});
</script>

<template>
  <div class="productList" v-if="collectionProductList.length > 0">
    <div class="item" v-for="item in collectionProductList" :key="item.id">
      <div
        class="cItem w40 nameBox cur"
        @click="jumpTo('/productDetail?id=' + item.product_id)"
      >
        <div class="coverUrl">
          <img :src="item.image" alt="" />
        </div>
        <div class="name">{{ item.store_name }}</div>
      </div>
      <div class="cItem right">
        <div class="delBox" @click="deleteProduct([item.product_id])">
          <el-icon><Delete /></el-icon>
          <span>取消收藏</span>
        </div>
      </div>
    </div>
  </div>

  <div class="noData" v-else>
    <el-empty description="暂无商品">
      <el-button type="primary" @click="jumpTo('/productList')">前往添加</el-button>
    </el-empty>
  </div>
</template>

<style lang="scss" scoped>
.topMenu {
  margin-top: 30px;
}

.searchBox {
  display: flex;
  margin-top: 15px;

  .label {
    line-height: 30px;
    margin-right: 20px;
  }
}

::v-deep .el-input__wrapper {
  border-radius: 0;
}

.inputBox {
  display: flex;

  .left {
    margin-right: 30px;
  }
}

::v-deep .el-checkbox__label {
  width: calc(1400px - 50px);
  display: flex;
  padding-left: 30px;
}

::v-deep .bottomInput .el-input__wrapper {
  background-color: rgba(0, 0, 0, 0);
  box-shadow: none;
  border-bottom: 1px solid #dcdcdc;
  margin-bottom: 10px;
}

.productList {
  width: 900px;
  .item {
    padding: 20px 0;
    box-sizing: border-box;
    height: 140px;
    width: 100%;
    display: flex;
    border-bottom: 1px solid #f3f3f3;
    justify-content: space-between;
  }
}

.cItem {
  color: #666;
  width: 25%;

  &.w40 {
    width: 40%;
  }
  &.w20 {
    width: 20%;
  }

  .name {
    width: 300px;
    white-space: normal;
    font-weight: bold;
    line-height: 20px;
    display: flex;
    align-items: center;
    color: #000;
  }

  &.right {
    text-align: right;
  }

  .coverUrl {
    width: 100px;
    height: 100px;

    img {
      width: 100%;
      height: 100%;
    }
  }

  &.nameBox {
    display: flex;
  }

  .color {
    margin-top: 20px;
    margin-bottom: 10px;
  }

  &.priceBox {
    display: flex;
    box-sizing: border-box;

    .price {
      color: #000;
      margin-right: 15px;
      margin-top: 30px;
      font-size: 16px;
    }

    .rate {
      padding: 5px 10px;
      background-color: rgb(41, 97, 152);
      color: #fff;
      box-sizing: border-box;
      height: 32px;
      margin-top: 24px;
    }
  }
}

.numBox {
  margin-top: 20px;
}

.delBox {
  margin-top: 26px;
  cursor: pointer;
  span {
    margin-left: 15px;
  }

  .el-icon {
    position: relative;
    top: 1px;
  }
}
</style>
